package by.bsu.rfe.belianko.euniversity.controller.command;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import by.bsu.rfe.belianko.euniversity.constants.ErrorMessages;
import by.bsu.rfe.belianko.euniversity.constants.LoggerMessages;
import by.bsu.rfe.belianko.euniversity.constants.PageNames;
import by.bsu.rfe.belianko.euniversity.dao.IAdmissionDAO;
import by.bsu.rfe.belianko.euniversity.dao.IStudentsDAO;
import by.bsu.rfe.belianko.euniversity.exceptions.CommandException;
import by.bsu.rfe.belianko.euniversity.exceptions.DaoTechnicalException;
import by.bsu.rfe.belianko.euniversity.model.DataBaseLayer;
import by.bsu.rfe.belianko.euniversity.model.Student;

/**
 * Performs the authorization of a user in the system
 * 
 * @author Vitali_Belianko
 * 
 */
public class LoginCommand implements Command {
	private static final String CAN_NOT_GET_STUDENT_BY_LOGIN = "Can not get student by login!";
	private static final String CAN_NOT_CHECK_POWER_OF_USER = "Can not check power of user";
	private static final String STUDENT_FAILED_TO_LOGIN = "Student  failed to login";
	private static final String RECEIVE_LOGIN = "Receive login ";
	private static final String ADMIN_LOGGED_IN = "Admin logged in";
	private static final String STUDENT_LOGGED_IN = "Student logged in";
	private static final String ADMISSION_STATE = "admissionState";
	private static final String COMPLETE = "complete";
	private static final String ADMIN_POWER = "admin";
	private static final String STUDENT_POWER = "student";
	private static final String ADMIN_ATTR_VALUE = "adminAttr";
	private static final String FIRST_REQUEST = "firstRequest";
	private static final String ADMIN_ATTR = "adminAttr";
	private static final String STUDENT = "student";
	private static final Logger LOGGER = Logger.getLogger(LoginCommand.class);
	private static final String ERROR_MESSAGE = "errorMessage";
	private static final IStudentsDAO studentsDao = DataBaseLayer.getStudentsDao();
	private static final IAdmissionDAO admissionDao = DataBaseLayer.getIAdmissionDAO();
	private static final String PASSWORD = "password";
	private static final String LOGIN = "login";

	/**
	 * Performs authorization of the user in the system.
	 * 
	 * @param request
	 * 
	 * @return admin page if power of user is admin, cabinet page if power of
	 *         user is admin, login page if power=null
	 * @throws CommandException 
	 */

	@Override
	public String execute(HttpServletRequest request) throws CommandException {
		LOGGER.debug(LoggerMessages.STARTED_COMMAND);

		String page;
		String firstRequestParam = request.getParameter(FIRST_REQUEST);
		LOGGER.debug(firstRequestParam);
		String login = request.getParameter(LOGIN);
		String password = request.getParameter(PASSWORD);
		String power = null;

		if (login != null && password != null) {
			try {
				power = studentsDao.checkForPower(login, password);
			} catch (DaoTechnicalException e) {
				LOGGER.error(e.getMessage());
				throw new CommandException(CAN_NOT_CHECK_POWER_OF_USER, e);
			}
			LOGGER.debug(RECEIVE_LOGIN + login + " " + power);

		}

		if (firstRequestParam != null) {
			page = PageNames.LOGIN_PAGE;

		} else if (STUDENT_POWER.equals(power)) {
			Student student = null;
			try {
				student = studentsDao.getStudentByLogin(login);
			} catch (DaoTechnicalException e) {
				LOGGER.error(e.getMessage());
				throw new CommandException(CAN_NOT_GET_STUDENT_BY_LOGIN, e);
			}
			request.getSession(true).setAttribute(STUDENT, student);
			checkForComplete(request);

			LOGGER.debug(STUDENT_LOGGED_IN + login);

			page = PageNames.CABINET_PAGE;

		} else if (ADMIN_POWER.equals(power)) {
			checkForComplete(request);

			request.getSession(true).setAttribute(ADMIN_ATTR, ADMIN_ATTR_VALUE);

			LOGGER.debug(ADMIN_LOGGED_IN);

			page = PageNames.ADMIN_PAGE;

		} else {
			request.setAttribute(ERROR_MESSAGE, ErrorMessages.LOGIN_FAILED);

			LOGGER.debug(STUDENT_FAILED_TO_LOGIN);

			page = PageNames.LOGIN_PAGE;
		}

		LOGGER.debug(LoggerMessages.FINISHED_COMMAND);

		return page;
	}

	private boolean checkForComplete(HttpServletRequest request) throws CommandException {

		boolean isComplete = false;
		String admissionState = null;
		try {
			admissionState = admissionDao.getAdmissionState();
		} catch (DaoTechnicalException e) {
			LOGGER.error(e.getMessage());
			throw new CommandException(ErrorMessages.CAN_NOT_GET_ADMISSION_STATE, e);
		}

		if (COMPLETE.equals(admissionState)) {
			request.getSession(true).setAttribute(ADMISSION_STATE, admissionState);
			isComplete = true;
		}

		return isComplete;
	}
}
